﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 参考线，就是在 SketchUp 中使用卷尺工具创建的虚线参考线
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUGuideLineRef 的包装</remarks>
    public class GuideLine : DrawingElement
    {
        #region API 函数
        /// <summary>
        /// 创建参考线段
        /// </summary>
        /// <param name="guide_line"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuideLineCreateFinite(out IntPtr guide_line, Point3D start, Point3D end);

        /// <summary>
        /// 创建参考直线（无限延伸）
        /// </summary>
        /// <param name="guide_line"></param>
        /// <param name="point"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuideLineCreateInfinite(out IntPtr guide_line, Point3D point, Vector3D direction);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="guide_line"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuideLineRelease(ref IntPtr guide_line);

        /// <summary>
        /// 获取参考线的起点、方向、是否无限延伸
        /// </summary>
        /// <param name="guide_line"></param>
        /// <param name="start"></param>
        /// <param name="direction"></param>
        /// <param name="isinfinite"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuideLineGetData(IntPtr guide_line, ref Point3D start, ref Vector3D direction, out bool isinfinite);
        #endregion

        internal GuideLine(IntPtr id) => Id = id;

        /// <summary>
        /// 使用两点创建一个参考线
        /// </summary>
        /// <param name="start">起点</param>
        /// <param name="end">终点</param>
        /// <returns></returns>
        public static GuideLine Create(Point3D start, Point3D end)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUGuideLineCreateFinite(out IntPtr id, start, end));
            return new GuideLine(id);
        }

        /// <summary>
        /// 使用一个点和方向创建一个无限长的参考线
        /// </summary>
        /// <param name="point">参考线上的一点</param>
        /// <param name="direction">方向</param>
        /// <returns></returns>
        public static GuideLine Create(Point3D point, Vector3D direction)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUGuideLineCreateInfinite(out IntPtr id, point, direction));
            return new GuideLine(id);
        }

        /// <summary>
        /// 获取参考线的信息
        /// </summary>
        /// <param name="start"></param>
        /// <param name="direction"></param>
        /// <param name="isInfinite"></param>
        public void GetInfo(out Point3D start, out Vector3D direction, out bool isInfinite)
        {
            start = Point3D.Origin;
            direction = new Vector3D(0, 0, 0);
            SketchUpExceptionHelper.ThrowSUResult(SUGuideLineGetData(Id, ref start, ref direction, out isInfinite));
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUGuideLineRelease(ref Id));
        }
    }
}